package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description NursingLevelServiceImpl
 * @Author chenchuang
 * @Date 2024-08-27
 */
@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Override
    public List<NursingLevel> getAll() {
        return nursingLevelMapper.getAll();

    }

    @Override
    public void add(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto, nursingLevel);
        nursingPlanMapper.updateCountAdd(nursingLevelDto.getPlanId());
        nursingLevelMapper.insert(nursingLevel);
    }

    @Override
    public PageResponse<NursingLevelVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingLevelVo> page = nursingLevelMapper.selectByPage(name,status);
        PageResponse<NursingLevelVo> pageResponse = new PageResponse(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    @Override
    public NursingLevelVo getById(Long id) {
        NursingLevelVo nursingLevelVo = nursingLevelMapper.getById(id);
        return nursingLevelVo;
    }

    @Override
    public void update(NursingLevelDto nursingLevelDto) {
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(nursingLevelDto, nursingLevel);
        NursingLevelVo byId = getById(nursingLevelDto.getId());
        nursingPlanMapper.updateCountDelete(byId.getPlanId());
        nursingPlanMapper.updateCountAdd(nursingLevelDto.getPlanId());
        nursingLevelMapper.update(nursingLevel);
    }

    @Override
    public void deleteById(Long id) {
        NursingLevelVo byId = getById(id);
        nursingPlanMapper.updateCountDelete(byId.getPlanId());
        nursingLevelMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id,status);
    }
}
